package com.compass.mobile.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.R
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentInspectBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.IndependentBillingBean
import com.compass.mobile.presenters.InspectPresenter
import com.compass.mobile.ui.dialog.CommonMessageDialog
import com.compass.mobile.ui.viewModel.IndependentBillingViewModel
import com.compass.mobile.widget.MyBridgeAdapter
import getStringFromResource


/**
 * Description： 检查Fragment
 * Date:2024/5/22 10:48
 */
class InspectFragment : BaseVBFragment<FragmentInspectBinding>() {
    private val independentBillingViewModel: IndependentBillingViewModel by activityViewModels()
    private var inspectList: MutableList<IndependentBillingBean> = mutableListOf()//包装集合
    private var inspectPageList: List<List<IndependentBillingBean>> = mutableListOf()
    private var inspectShowList: List<IndependentBillingBean> = mutableListOf()//展示出来的数据


    private var pageIndex = 0 // 当前下标
    private val pageSize = 6 // 每页数量
    private var hasPageNumber=0 //有多少页
    private var isHaveNext = true // 是否有更多数据

    private lateinit var arrayObjectAdapter: ArrayObjectAdapter
    override fun initView(view: View, savedInstanceState: Bundle?) {
        specialColumn()
        FocusViewUtils.scaleView(mBinding?.tvPrevious, 1)
        FocusViewUtils.scaleView(mBinding?.tvNextPage, 1)
    }


    override fun initData() {
        independentBillingViewModel.inspectionListLiveData.observe(this) {
            it?.let { list ->
                inspectList = list
                inspectPageList = inspectList.chunked(pageSize)
                if (inspectPageList.isNotEmpty()) {
                    hasPageNumber=inspectPageList.size-1
                    loadData(pageIndex)
                }
            }
            dismissLoading()
        }
    }

    override fun intListener() {
        mBinding?.tvPrevious?.onClick {
            if (pageIndex > 0) {
                pageIndex--
                loadData(pageIndex)
            } else {
                showToast("没有上一页了")
            }
        }

        mBinding?.tvNextPage?.onClick {
            if (isHaveNext) {
                pageIndex++
                loadData(pageIndex)
            } else {
                showToast("没有下一页了")
            }
        }
    }


    private fun loadData(pageIndex: Int) {
        isHaveNext = pageIndex<hasPageNumber
        inspectShowList= inspectPageList[pageIndex]
        arrayObjectAdapter.clear()
        arrayObjectAdapter.addAll(0, inspectShowList)
    }


    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
            val item = inspectShowList[position]

            CommonMessageDialog.Builder(requireActivity())
                .setTitle("温馨提示")
                .setMessage("确认要添加当前项目吗?")
                .setConfirm(getStringFromResource(R.string.default_confirm))

                .setCancel(getString(R.string.default_cancel))
                .setonCancelListener {
                    it?.dismiss()
                }
                .setonConfirmListener {
                    if (!isAddInspect(item)) {
                        independentBillingViewModel.selectedList.add(item)
                        independentBillingViewModel.selectedListLiveData.value =
                            independentBillingViewModel.selectedList
                    } else {
                        showToast("该检查已添加，不需要重复添加")
                    }


                    it?.dismiss()
                }.create().show()

        }
    }


    /**
     * 判断是否添加过了
     */
    private fun isAddInspect(independentBillingBean: IndependentBillingBean): Boolean {
        for (item in independentBillingViewModel.selectedList) {
            if (item.name == independentBillingBean.name) {
                return true
            }
        }
        return false
    }

    @SuppressLint("RestrictedApi")
    fun specialColumn() {
        mBinding?.caseItem?.setNumColumns(2)
        mBinding?.caseItem?.horizontalSpacing = dpToPx(10)
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)
        val inspectPresenter = InspectPresenter()
        arrayObjectAdapter = ArrayObjectAdapter(inspectPresenter)
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )
        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, inspectShowList)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )


    }


}
